package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import po.Account;
import po.Cart;
import po.Dish;
import po.Orders;
import service.OrderService;
import service.impl.OrderServiceImpl;

/**
 * Servlet implementation class OrderServlet
 */
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private OrderService orderService = new OrderServiceImpl();
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action.equals("checkOut"))
			checkOut(request, response);
		else if (action.equals("pay"))
			pay(request, response);
		else if(action.equals("all"))
			getAllOrders(request,response);
	}

	private void getAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("account");
		List<Orders> orderList = orderService.getAllOrder(account.getAid(), request);
		request.setAttribute("orderList", orderList);
		request.getRequestDispatcher("/list.jsp").forward(request, response);
	}

	@SuppressWarnings("null")
	private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpSession session = request.getSession();
		Account account = (Account)session.getAttribute("account");
		String aid = account.getAid();
		System.out.println("===================" + aid);
		Orders orders = null;
		Cart cart = null;
		Integer oid = orderService.getOid();
		Map<Integer,Integer> carts = (Map<Integer,Integer>)session.getAttribute("carts");
		if(carts != null && account != null){
			for(Integer i:carts.keySet()){
				cart = new Cart();
				cart.setAid(account.getAid());
				cart.setDid(i);
				cart.setCount(carts.get(i));
				orderService.addOrder(cart, oid);
			}
		}
		session.removeAttribute("carts");
		response.sendRedirect(request.getContextPath() + "/client/listOrder");
		//request.getRequestDispatcher("/client/listOrder").forward(request, response);
	}

	private void checkOut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("account");
		Cart cart = null;
		Dish dish = null;
		if (account == null) {
			String message = String.format("您未登录，请登录后再提交订单！！<meta http-equiv='refresh' content='2;url=%s'",
					request.getContextPath() + "login.jsp");
			request.setAttribute("message", message);
			request.getRequestDispatcher("/message.jsp").forward(request, response);
		}else{
			List<Cart> cartList = new ArrayList<Cart>();
			Map<Integer,Integer> carts = (Map<Integer,Integer>)session.getAttribute("carts");
			if(carts != null && account != null){
				Map<Integer,Dish> dishMap = (Map<Integer,Dish>)request.getServletContext().getAttribute("dishMap");
				for(Integer i : carts.keySet()){
					cart = new Cart();
					dish = dishMap.get(i);
					cart.setAid(account.getAid());
					cart.setDid(i);
					cart.setCount(carts.get(i));
					cart.setDish(dish);
					cartList.add(cart);
				}
			}
			request.setAttribute("cartList", cartList);
			request.getRequestDispatcher("/pay.jsp").forward(request, response);
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
